package cn.stylefeng.guns.modular.ws.sso.action;

import cn.stylefeng.guns.modular.ws.sso.proxy.pullSoapAccountService.Exception;
import cn.stylefeng.guns.modular.ws.sso.proxy.pullSoapAccountService.PullSoapAccountServiceStub;
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser;
import cn.stylefeng.guns.sys.modular.rest.service.RestUserService;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.apache.axis2.AxisFault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/api")
public class SysnUserController {
    @Autowired
    private UserService userService;

    @Resource
    private RestUserService restUserService;

    @Autowired
    private DeptService deptService;

    @GetMapping("/userSync")
    @ResponseBody
    public boolean user_sync(@RequestParam("deptId") Long deptId) {
        System.out.println("fengtyfenty");
        UserDto user = new UserDto();
    /*    for(int i=0; i<3; i++) {

            System.out.println("user11:"+user.toString());
            user.setName("冯大宇");
            user.setSex("女");
            user.setEmail("123@qq.com");
            user.setAccount("fengty111");
            user.setPassword("309216366");
            user.setAvatar("gdjt");
            this.userService.addUser(user);
            user.setName("冯同宇");
            user.setSex("男");
            user.setEmail("123@qq.com");
            user.setAccount("fengty111");
            user.setPassword("309216366");
            user.setAvatar("gdjt");
            this.userService.addUser(user);
        return true ;
        }*/
        try {
            String targetUrl = "http://ssotest.kingser.com:90/esc-ws/services/pullSoapAccountService" ;
            PullSoapAccountServiceStub stub = new PullSoapAccountServiceStub(targetUrl) ;
            PullSoapAccountServiceStub.QueryAccountModelE  queryAccountModelE = new PullSoapAccountServiceStub.QueryAccountModelE() ;
            PullSoapAccountServiceStub.QueryAccountModel queryAccountModel= new PullSoapAccountServiceStub.QueryAccountModel() ;
            PullSoapAccountServiceStub.RequestAccountInfoModel requestAccountInfoModel = new PullSoapAccountServiceStub.RequestAccountInfoModel() ;
            requestAccountInfoModel.setKey("11AAC64B2052093A3D77C0A419680C7F0AC066D1F790AE9B4ABD58C26ADDCA5E3A018528");
            requestAccountInfoModel.setAppcode("guns");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

            int y = 2;
            for (int i = 1; i < y; i++) {
                requestAccountInfoModel.setPageNum(String.valueOf(i));
                queryAccountModel.setArg0(requestAccountInfoModel);

                queryAccountModel.setArg0(requestAccountInfoModel);
                queryAccountModelE.setQueryAccountModel(queryAccountModel);
                PullSoapAccountServiceStub.QueryAccountModelResponseE queryAccountModelResponseE1 = stub.queryAccountModel(queryAccountModelE) ;
                PullSoapAccountServiceStub.QueryAccountModelResponse queryAccountModelResponse = queryAccountModelResponseE1.getQueryAccountModelResponse() ;
                PullSoapAccountServiceStub.PullQueryAccountModel pullQueryAccountModel = queryAccountModelResponse.get_return() ;
                PullSoapAccountServiceStub.AccountModel[] accountModels = pullQueryAccountModel.getRetList();
                if(accountModels == null || accountModels.length == 0){
                    System.out.println("部门人员为空！"+deptId.toString());
                }else{
                    for (PullSoapAccountServiceStub.AccountModel Model : accountModels) {

                        if (!"".equals(Model.getOprId())) {
                            // 判断账号是否重复
                            RestUser theUser = restUserService.getByAccount(Model.getOprId());
                            if (theUser == null) {
                                user.setAccount(Model.getOprId());
                                user.setName(Model.getName());
                                user.setSex(Model.getSex().replace("W", "F"));
                                user.setEmail(Model.getEmailAddr());
                                user.setPhone(Model.getPhone1());
                                //user.setDeptId(new Long(Model.getDeptId()));  //fengty 新增部门  20201111
                                user.setDeptId(new Long(deptId));  //fengty 新增部门  20201111
                                if(!"".equals(Model.getBirthdate())&& Model.getBirthdate() != null){
                                    user.setBirthday(df.parse(Model.getBirthdate()));

                                }
                                user.setStatus(Model.getStatus().replace("0", "ENABLE"));
                                user.setPassword("123456");
                                user.setAvatar("kingser");

                                this.userService.addUser(user);
                            }else{
                                System.out.println("名称:"+Model.getName()+" 重复！");
                            }


                        }
                    }
                }
                if ("1".equals(pullQueryAccountModel.getHaseNext())) {
                    y = y + 1;
                }
                System.out.println("i:" + i + "--y:" + y);




            }

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
            return false;
        } catch (RemoteException e) {

            e.printStackTrace();
            return false;
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) {
        try {
            String targetUrl = "http://ssotest.kingser.com:90/esc-ws/services/pullSoapAccountService" ;
            PullSoapAccountServiceStub stub = new PullSoapAccountServiceStub(targetUrl) ;
            PullSoapAccountServiceStub.QueryAccountModelE  queryAccountModelE = new PullSoapAccountServiceStub.QueryAccountModelE() ;
            PullSoapAccountServiceStub.QueryAccountModel queryAccountModel= new PullSoapAccountServiceStub.QueryAccountModel() ;
            PullSoapAccountServiceStub.RequestAccountInfoModel requestAccountInfoModel = new PullSoapAccountServiceStub.RequestAccountInfoModel() ;
            requestAccountInfoModel.setKey("11AAC64B2052093A3D77C0A419680C7F0AC066D1F790AE9B4ABD58C26ADDCA5E3A018528");
            requestAccountInfoModel.setAppcode("guns");
            requestAccountInfoModel.setPageNum("1");
            queryAccountModel.setArg0(requestAccountInfoModel);
            queryAccountModelE.setQueryAccountModel(queryAccountModel);
            PullSoapAccountServiceStub.QueryAccountModelResponseE queryAccountModelResponseE1 = stub.queryAccountModel(queryAccountModelE) ;
            PullSoapAccountServiceStub.QueryAccountModelResponse queryAccountModelResponse = queryAccountModelResponseE1.getQueryAccountModelResponse() ;
            PullSoapAccountServiceStub.PullQueryAccountModel pullQueryAccountModel = queryAccountModelResponse.get_return() ;
            PullSoapAccountServiceStub.AccountModel[] accountModels = pullQueryAccountModel.getRetList();
            for(PullSoapAccountServiceStub.AccountModel Model : accountModels){
                System.out.println( Model.getAccountName() );
            }


        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}
